<template>
    <!-- 添加或修改部门对话框 -->
    <a-modal :title="title" v-model:visible="open" width="700px" append-to-body>
        <a-form ref="deptRef" :model="form" :rules="rules">
            <a-row>
                <a-col :span="24" v-if="form.parentId !== 0">
                    <a-form-item label="上级部门" prop="parentId">
                        <a-tree-select v-model="form.parentId" :data="deptOptions"
                            :props="{ value: 'deptId', label: 'deptName', children: 'children' }" value-key="deptId"
                            placeholder="选择上级部门" check-strictly />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="部门名称" prop="deptName">
                        <a-input v-model="form.deptName" placeholder="请输入部门名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="显示排序" prop="orderNum">
                        <a-input-number v-model="form.orderNum" controls-position="right" :min="0" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="负责人" prop="leader">
                        <a-input v-model="form.leader" placeholder="请输入负责人" maxlength="20" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系电话" prop="phone">
                        <a-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="邮箱" prop="email">
                        <a-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="部门状态">
                        <a-radio-group v-model="form.status">
                            <a-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{
                                dict.label
                                }}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script setup name="DeptForm">
import { addDept, listDept, updateDept } from "@/api/system/dept";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const deptList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const deptOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
    form: {},
    queryParams: {
        deptName: undefined,
        status: undefined
    },
    rules: {
        parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
        deptName: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
        orderNum: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
        email: [{ type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
        phone: [{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }]
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}
/** 表单重置 */
function reset() {
    form.value = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: 0,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0"
    };
    proxy.resetForm("deptRef");
}
function init(method, deptId) {
    reset();
    open.value = true;
    if (method == 'add') {
        listDept().then(response => {
            deptOptions.value = proxy.handleTree(response.data, "deptId");
        });
        form.value.parentId = deptId
        title.value = "添加部门";
    } else if (method == 'edit') {
        listDeptExcludeChild(row.deptId).then(response => {
            deptOptions.value = proxy.handleTree(response.data, "deptId");
        });
        title.value = "修改部门";
    }
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["deptRef"].validate(valid => {
        if (valid) {
            if (form.value.deptId != undefined) {
                updateDept(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addDept(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

defineExpose({
    init
});
</script>